package com.weipeep.sys.controller;


import java.util.*;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.weipeep.common.base.BaseController;
import com.weipeep.sys.domain.WebUserDO;
import com.weipeep.sys.service.WebUserService;
import com.weipeep.sys.vo.DemandVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.DemandDO;
import com.weipeep.sys.service.DemandService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-25 00:42:47 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/demand")
public class DemandController extends BaseController {
	@Autowired
	private DemandService demandService;
	@Autowired
	private WebUserService webUserService;
	@GetMapping()
//	@RequiresPermissions("sys:demand:demand")
	String Demand(){
	    return "sys/demand/demand";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("sys:demand:demand")
	public Result<Page<DemandVo>> list(Integer pageNumber, Integer pageSize, DemandDO demandDTO){
		// 查询列表数据
        Page<DemandDO> page = new Page<>(pageNumber, pageSize);
		demandDTO.setUserId(getUserId());
		String model = demandDTO.getModel();
		demandDTO.setModel(null);
        Wrapper<DemandDO> wrapper = new EntityWrapper<DemandDO>(demandDTO);
        if (model!=null && model.trim().length()>0){
        	wrapper.like("model",model);
		}
		wrapper.orderBy("create_time desc");
        page = demandService.selectPage(page, wrapper);
        int total = demandService.selectCount(wrapper);


		List<DemandDO> demandDOList = page.getRecords();
		Set<Long> userIds = Sets.newConcurrentHashSet();
		Page<DemandVo> pageresult = new Page<>(pageNumber, pageSize);
		demandDOList.forEach(xunJiaDO -> userIds.add(xunJiaDO.getUserId()));
		if (userIds.size()>0){
			List<DemandVo> resultList = Lists.newArrayList();
			List<WebUserDO> webUserDOList = webUserService.selectBatchIds(Lists.newArrayList(userIds));
			Map<Long,WebUserDO> map = Maps.newHashMap();
			for (WebUserDO webUserDO:webUserDOList){
				map.put(webUserDO.getId(),webUserDO);
			}
			for(DemandDO demandDO:demandDOList){
				DemandVo demandVo = new DemandVo();
				if (map.get(demandDO.getUserId())!=null){
					demandVo.setCompany(map.get(demandDO.getUserId()).getCompany());
					demandVo.setUserAccount(map.get(demandDO.getUserId()).getUserAccount());
					demandVo.setMobile(map.get(demandDO.getUserId()).getMobile());
					demandVo.setQq(map.get(demandDO.getUserId()).getQq());
					demandVo.setTelephone(map.get(demandDO.getUserId()).getTelephone());
				}
				demandVo.setModel(demandDO.getModel());
				demandVo.setUserId(demandDO.getUserId());
				demandVo.setCreateTime(demandDO.getCreateTime());
				demandVo.setNumber(demandDO.getNumber());
				demandVo.setType(demandDO.getType());
				demandVo.setBrand(demandDO.getBrand());
				demandVo.setNote(demandDO.getNote());
				demandVo.setId(demandDO.getId());
				demandVo.setReceivePrice(demandDO.getReceivePrice());
				resultList.add(demandVo);
			}
			pageresult.setRecords(resultList);
		}
		pageresult.setTotal(total);
		return Result.ok(pageresult);
	}

	@GetMapping("/add")
//	@RequiresPermissions("sys:demand:add")
	String add(){
	    return "sys/demand/add";
	}

	@GetMapping("/edit/{id}")
//	@RequiresPermissions("sys:demand:edit")
	String edit(@PathVariable("id") Long id,Model model){
		DemandDO demand = demandService.selectById(id);
		model.addAttribute("demand", demand);
	    return "sys/demand/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
//	@RequiresPermissions("sys:demand:add")
	public Result<String> save( DemandDO demand){
		demand.setUserId(getUserId());
		demand.setCreateTime(Calendar.getInstance().getTime());
		if(demandService.insert(demand)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("sys:demand:edit")
	public Result<String>  update( DemandDO demand){
		demandService.updateById(demand);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("sys:demand:remove")
	public Result<String>  remove( Long id){
		if(demandService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
//	@RequiresPermissions("sys:demand:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		demandService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
